using LeaRun.Application.Busines.SystemManage;
using LeaRun.Application.Entity.SystemManage;
using LeaRun.Util;
using LeaRun.Util.WebControl;
using System;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Web.Mvc;

namespace LeaRun.Application.Web.Areas.SystemManage.Controllers
{
	public class DataSourceController : MvcControllerBase
	{
		private DataSourceBLL datasourcebll = new DataSourceBLL();
		[HttpGet]
		public ActionResult Index()
		{
			return base.View();
		}
		[HttpGet]
		public ActionResult Form()
		{
			return base.View();
		}
		[HttpGet]
		public ActionResult GetPageListJson(Pagination pagination, string queryJson)
		{
			Stopwatch stopwatch = CommonHelper.TimerStart();
			var data = new
			{
				rows = this.datasourcebll.GetPageList(pagination, queryJson),
				total = pagination.total,
				page = pagination.page,
				records = pagination.records,
				costtime = CommonHelper.TimerEnd(stopwatch)
			};
			return this.ToJsonResult(data);
		}
		[HttpGet]
		public ActionResult GetListJson(string queryJson)
		{
			IEnumerable<DataSourceEntity> list = this.datasourcebll.GetList(queryJson);
			return this.ToJsonResult(list);
		}
		[HttpGet]
		public ActionResult GetFormJson(string keyValue)
		{
			DataSourceEntity entity = this.datasourcebll.GetEntity(keyValue);
			return this.ToJsonResult(entity);
		}
		[HttpGet]
		public ActionResult GetTableData(string dbLinkId, string tableName)
		{
			DataTable tableDataList = this.datasourcebll.GetTableDataList(dbLinkId, tableName);
			return this.ToJsonResult(tableDataList);
		}
		[AjaxOnly(false), HttpPost, ValidateAntiForgeryToken]
		public ActionResult RemoveForm(string keyValue)
		{
			this.datasourcebll.RemoveForm(keyValue);
			return this.Success("删除成功。");
		}
		[AjaxOnly(false), HttpPost, ValidateAntiForgeryToken]
		public ActionResult TestData(string strEntity, string queryJson)
		{
			DataBaseLinkBLL arg_12_0 = new DataBaseLinkBLL();
            DataSourceEntity dataSourceEntity = LeaRun.Util.Json.ToObject<DataSourceEntity>(strEntity);
			DataBaseLinkEntity entity = arg_12_0.GetEntity(dataSourceEntity.DbId);
			string text = this.datasourcebll.TestData(entity.DbType, entity.DbConnection, dataSourceEntity.Strsql);
			return base.Content(text);
		}
		[AjaxOnly(false), HttpPost, ValidateAntiForgeryToken]
		public ActionResult SaveForm(string keyValue, DataSourceEntity entity)
		{
			this.datasourcebll.SaveForm(keyValue, entity);
			return this.Success("操作成功。");
		}
	}
}
